package cn.lili.modules.search.serviceimpl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.lili.common.utils.BeanUtil;
import cn.lili.common.vo.PageVO;
import cn.lili.modules.goods.entity.dos.Goods;
import cn.lili.modules.goods.entity.dos.GoodsSku;
import cn.lili.modules.goods.entity.dto.GoodsSearchParams;
import cn.lili.modules.goods.service.GoodsService;
import cn.lili.modules.goods.service.GoodsSkuService;
import cn.lili.modules.search.entity.dos.EsGoodsIndex;
import cn.lili.modules.search.entity.dto.EsGoodsSearchDTO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * ES商品搜索业务层
 *
 * @author paulG
 * @since 2020/10/15
 **/
@Slf4j
@Service
public class GoodsSearchServiceImpl {

    // 最小分词匹配
    private static final String MINIMUM_SHOULD_MATCH = "20%";

    private static final String ATTR_PATH = "attrList";
    private static final String ATTR_VALUE = "attrList.value";
    private static final String ATTR_NAME = "attrList.name";
    private static final String ATTR_SORT = "attrList.sort";
    private static final String ATTR_BRAND_ID = "brandId";
    private static final String ATTR_BRAND_NAME = "brandNameAgg";
    private static final String ATTR_BRAND_URL = "brandUrlAgg";
    private static final String ATTR_NAME_KEY = "nameList";
    private static final String ATTR_VALUE_KEY = "valueList";

    @Resource
    private GoodsService goodsService;
    @Resource
    private GoodsSkuService goodsSkuService;

    /**
     * 根据商品查询，只返回商品信息和一条规格信息
     *
     * @param searchDTO 搜索参数
     * @param pageVo    分页参数
     * @return 查询结果
     */
    public Page<EsGoodsIndex> searchGoodsByPage(EsGoodsSearchDTO searchDTO, PageVO pageVo) {
        Page<EsGoodsIndex> targetPage = new Page<>();
        // 查询规格
        GoodsSearchParams goodsSearchParams = new GoodsSearchParams();
        // 复制值
        BeanUtil.copyProperties(searchDTO, goodsSearchParams);
        BeanUtil.copyProperties(pageVo, goodsSearchParams);
        goodsSearchParams.setGoodsName(searchDTO.getKeyword());
        // 分页查询
        IPage<Goods> goodsPage = goodsService.queryByParams(goodsSearchParams);
        List<Goods> records = goodsPage.getRecords();
        if (CollUtil.isEmpty(records)) {
            return targetPage;
        }

        // 根据产品ID查询关联
        List<String> goodsIds = records.stream().map(Goods::getId).toList();

        List<GoodsSku> skuList = goodsSkuService.listByGoodsIds(goodsIds);

        BeanUtil.copyProperties(goodsPage, targetPage);
        List<EsGoodsIndex> esGoodsIndices = new ArrayList<>();
        for (Goods goods : records) {
            EsGoodsIndex esGoodsIndex = new EsGoodsIndex();
            // 拷贝
            BeanUtil.copyProperties(goods, esGoodsIndex);
            // 筛选产品，有多个，只筛选第一个
            skuList.stream().filter(item ->
                            ObjectUtil.equals(goods.getId(), item.getGoodsId())).findFirst()
                    .ifPresent(item -> BeanUtil.copyProperties(item, esGoodsIndex));
            esGoodsIndices.add(esGoodsIndex);
        }
        targetPage.setRecords(esGoodsIndices);
        return targetPage;
    }

}
